//index.js
//获取应用实例
var app = getApp();

Page({
  data: {
    autoplay: false,
    interval: 3000,
    duration: 1000,
    fishDetail: {},
    swiperCurrent: 0,
    shopNum: 0,
    hideShopPopup: true,
    buyNumber: 0,
    buyNumMax: 0,
    imgDomain: '',
    showUserModal: false,
    signLoading: false
  },

  //事件处理函数
  swiperchange: function (e) {
    //console.log(e.detail.current)
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  onLoad: function (e) {
    if (e.inviter_id) {
      wx.setStorage({
        key: 'inviter_id_' + e.id,
        data: e.inviter_id
      })
    }
    var that = this;
    that.setData({ imgDomain: app.globalData.SiteDomain });
    that.getDetail(e.id);
    console.log(e.id);
    // this.reputation(e.id);

  },
  getDetail: function (id) {
    var that=this;
    wx.request({
      url: app.globalData.Domain + '/fish/detail',
      data: {
        id: id
      },
      success: function (res) {
        if (res.data.code == 0) {
          console.log(res.data.data);
          that.setData({
            fishDetail: res.data.data,
            selectSizePrice: res.data.data.fields.sell_price,
            buyNumMax: res.data.data.fields.stock_quantity, 
            buyNumber: (res.data.data.fields.stock_quantity > 0) ? 1 : 0
          });
          // WxParse.wxParse('article', 'html', res.data.data.content, that, 5);
        }

        else {
          wx.showModal({
            title: '哪有鱼提示',
            content: res.data.msg,
            showCancel:false
          })  
        }
      }
    })
  },
  toSignIn: function () {
    //
    var that = this;
    that.setData({ signLoading: true });
    app.getUserInfo(function (res) {
      that.setData({ signLoading: false });
      if (res == null) {
        that.setData({
          showUserModal: true
        })
      }
      else {
        console.log(res);
      }
    });
  },
  onShareAppMessage: function () {
    return {
      title: this.data.fishDetail.basicInfo.name,
      path: '/pages/fish/index?id=' + this.data.fishDetail.id + '&inviter_id=' + app.globalData.uid,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  onGotUserInfo: function (res) {
    var that = this;
    that.hideUserModal();
    console.log(res);
    if (res.detail.userInfo == null) {
      wx.showModal({
        title: '哪有鱼提示',
        content: '我很有用的，请不要拒绝我哦，再来一次吧',
        showCancel: false,
        success: function (res) {
          that.setData({ showUserModal: true });
        }
      })
    } else {
      app.globalData.userInfo = res.detail.userInfo;
      //登录获取token
      app.login(function (res) {
        app.globalData.token = res.token;
        //do somthing
      });
    }

  },
  reputation: function (goodsId) {
    var that = this;
    wx.request({
      url: app.globalData.Domain + '/fish/commits',
      data: {
        goodsId: goodsId
      },
      success: function (res) {
        if (res.data.code == 0) {
          //console.log(res.data.data);
          that.setData({
            reputation: res.data.data
          });
        }
      }
    })
  },
  /**
   * 隐藏模态对话框
   */
  hideUserModal: function () {
    this.setData({
      showUserModal: false
    });
  },
  getVideoSrc: function (videoId) {
    that.setData({
      videoMp4Src: videoId
    });
    return;
    var that = this;
    wx.request({
      url: app.globalData.Domain + '/media/video/detail',
      data: {
        videoId: videoId
      },
      success: function (res) {
        if (res.data.code == 0) {
          that.setData({
            videoMp4Src: res.data.data.fdMp4
          });
        }
      }
    })
  }
})
